package com.shadow.baiqun.delegate.send.detail;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.shadow.core.util.date.DateUtils;
import com.shadow.ui.recycler.ObjConverter;

import java.util.ArrayList;

/**
 * @author zy
 *         on 2018/6/26
 */
public class SendDetailConverter {

    public ArrayList<SendDetailEntity> convert(String response, OnCalculateListener listener) {
        ArrayList<SendDetailEntity> list = new ArrayList<>();
        try {
            final JSONArray jsonArray = JSONObject.parseObject(response)
                    .getJSONArray("rows");
            final int size = jsonArray.size();
            int notCheckNumber = 0;
            for (int i = 0; i < size; i++) {
                final JSONObject object = jsonArray.getJSONObject(i);
                SendDetailEntity sendDetailEntity = new ObjConverter<SendDetailEntity>().
                        convert(object.toJSONString(), new TypeReference<SendDetailEntity>() {
                        });
                String carTime = sendDetailEntity.getCarTime();
                if(carTime == null || carTime.isEmpty()){
                    sendDetailEntity.setCarTime("未发货");
                    notCheckNumber++;
                }else {
                    sendDetailEntity.setCarTime(DateUtils.longToString(
                            Long.valueOf(carTime)));
                }
                list.add(sendDetailEntity);
            }
            if (listener != null) {
                listener.onNotCheckNumber(notCheckNumber,size);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public interface OnCalculateListener {
        void onNotCheckNumber(int number,int total);
    }
}
